﻿from xlrd import open_workbook,cellname

class DataSet:

    def __init__(self, file, vars):
        self.index = 1
        self.book = open_workbook(file)
        self.sheet = self.book.sheet_by_index(0)

        self.dict={}
        for name in vars:
            for col in range(self.sheet.ncols):
                if self.sheet.cell(0,col).value == unicode(name):
                    self.dict.update({name:col})

    def getVar(self, name):
        return self.sheet.cell(self.index, self.dict[name]).value

    def getVarAt(self, name, index):
        raise BaseException("not implemented")

    def resetIterator(self):
        self.index = 1
        pass

    def nextItem(self):
        self.index += 1
        if self.index < self.sheet.nrows:
            return True
        else:
            self.index = 1
            return False


if __name__ == '__main__':
    import trees
    ds = DataSet('simple.xls', ['x','y','z'])
    gp = ['+',1,'x']

    while 1:
        print "x=",ds.getVar('x'),"   f(x)=", trees.evalTree(gp, ds)
        if not ds.nextItem():
            break
    ds.resetIterator()
